%Model calibration
%Produces Tables 1 through 4 of Appendix B

clear
delta = .01; %housing depreciation
alpha = [.14, .21] ; %fraction of non-borrowers in high and low states
pl_share = [.25,.07]; %private-label share in high and low states
alpha_h = [.332,.25] ; %fraction of high-income borrowers in high and low states
phi_bh = [.98,.95]; %high-income borrower endowment receival probability in high and low state
phi_bl = [.88,0.75]; %low-income borrower endowment receival probability in high and low state
e = .95; %borrower endowment
c = 1; %house price supply parameter
N=2; %number of large "banks"
q_s = [.9,.1;.01,.99]; % state transition matrix
beta =.94;
pl_benchmark = pl_share(2);
%%
%Benchmark calibration

u=cell(4,1); %1 indexes benchmark, 2 indexes counterfactual 1 (no propping up), 3 indexes counterfactual 2 (high house price expectations), 4 indexes counterfactual 3 (no private label exit)

%benchmark
    K=1; %propping-up active in vfi_benchmark 
    %policy and prices given outstanding loans and state
    [g_temp{1},index_temp{1},p_temp{1}] = vfi_benchmark(K,N,alpha,pl_share, alpha_h, phi_bh, phi_bl, q_s, beta,delta,c,e);

%Counterfactual 1
    K=0; %propping-up inactive in vfi_benchmark
    
    %policy and prices given outstanding loans and state
    [g_temp{2},index_temp{2},p_temp{2}] = vfi_benchmark(K,N,alpha,pl_share, alpha_h, phi_bh, phi_bl, q_s, beta,delta,c,e);


%Counterfactual 2
   K=0;  %propping-up inactive in vfi_benchmark_highex
   ce_h = c*.33;
   ce_l=0;
   ce=[ce_h,ce_l]; %higher house price expectations parameters
   
%policy and prices given outstanding loans and state
    [g_temp{3},index_temp{3},p_temp{3}] =vfi_benchmark_highex(ce,K,N,alpha,pl_share, alpha_h, phi_bh, phi_bl, q_s, beta,delta,c,e);

%Counterfactual 3
   K=1; %propping-up active in vfi_benchmark
   pl_share(2) =.2; %PL share stays high in low shate

%policy and prices given outstanding loans and state
    [g_temp{4},index_temp{4},p_temp{4}] = vfi_benchmark(K,N,alpha,pl_share, alpha_h, phi_bh, phi_bl, q_s, beta,delta,c,e);

%Choose initial value of loans
k0=50;

for i=1:4 %loops over scenarios
    pl_share(2) =pl_benchmark; 
    if i == 4
    pl_share(2) =.2;
    end
%2006H1 -- first high shock 
m(1,i) = N*g_temp{i}(k0,1); %total loans
m_l(1,i) = max(m(1,i)-alpha_h(1),0); %loans to low-quality borrowers
m_h(1,i)=min(m(1,i),alpha_h(1)); %loans to high-quality borrowers
p(1,i) = c*(m(1,i)+alpha(1)+pl_share(1)); %house prices
k(1,1)=k0;

%2006H2 -- second high shock
    m(2,i)= N*g_temp{i}(k(1,1),1);
    p(2,i) = p_temp{i}(k(1,1),1);
    k(2,i)= index_temp{i}(k(1,1),1); 
    m_l(2,i) = max(m(2,i)-alpha_h(1),0);
    m_h(2,i)=min(m(2,i),alpha_h(1));

%2007H1 - first low shock
   m(3,i)= N*g_temp{i}(k(2,1),2);
   p(3,i) = p_temp{i}(k(2,1),2);
   k(3,i)= index_temp{i}(k(2,1),2);
   m_l(3,i) = max(m(3,i)-alpha_h(2),0);
   m_h(3,i)=min(m(3,i),alpha_h(2));
   
%2007H2 - second low shock
   m(4,i)= N*g_temp{i}(k(3,1),2);
   p(4,i) = p_temp{i}(k(3,1),2);
   k(4,i)= index_temp{i}(k(3,1),2);
   m_l(4,i) = max(m(4,i)-alpha_h(2),0);
   m_h(4,i)=min(m(4,i),alpha_h(2));
   
%2008H1 - third low shock
   m(5,i)= N*g_temp{i}(k(4,1),2);
   p(5,i) = p_temp{i}(k(4,1),2);
   k(5,i)= index_temp{i}(k(4,1),2);
   m_l(5,i) = max(m(5,i)-alpha_h(2),0);
   m_h(5,i)=min(m(5,i),alpha_h(2));
  
%2008H2 - fourth low shock
   m(6,i)= N*g_temp{i}(k(5,1),2);
   p(6,i) = p_temp{i}(k(5,1),2);
   m_l(6,i) = max(m(6,i)-alpha_h(2),0);
   m_h(6,i)=min(m(6,i),alpha_h(2));
end
%%
% model-implied parameters (all 4 scenarios)

   %gse-acquisitions 06 and 07 and 08
   acquisitions_06 = m(1,:)+m(2,:);
   acquisitions_07 = m(3,:)+m(4,:);
   acquisitions_08 = m(5,:)+m(6,:);

   %high-risk fraction 06, 07 and 08
   li_fraction_06=(m_l(1,:)+m_l(2,:))./(m(1,:)+m(2,:)); 
   li_fraction_07=(m_l(3,:)+m_l(4,:))./(m(3,:)+m(4,:)); 
   li_fraction_08=(m_l(5,:)+m_l(6,:))./(m(5,:)+m(6,:)); 
   
   %growth in high_risk acquisitions 
   li_gr_06_07 = (m_l(3,:)+m_l(4,:)-m_l(2,:)-m_l(1,:))./(m_l(2,:)+m_l(1,:)); 
   li_gr_07_08 = (m_l(5,:)+m_l(6,:)-m_l(4,:)-m_l(3,:))./(m_l(3,:)+m_l(4,:)); 
   
   %house price fall 06-08
   hp_fall_06_08 = (p(6,:)-p(1,:))./p(1,:);
   
   %default vintage 06,07 and 08
   def_06=  m_l(2,:)*(1-phi_bl(2))+m_l(1,:)*(1-phi_bl(1))+m_h(2,:)*(1-phi_bh(2))+m_h(1,:)*(1-phi_bh(1)) ;
   def_07 = (m_l(3,:)+m_l(4,:))*(1-phi_bl(2))+(m_h(3,:)+m_h(4,:)) *(1-phi_bh(2));
   def_08 = (m_l(5,:)+m_l(6,:))*(1-phi_bl(2))+(m_h(5,:)+m_h(6,:)) *(1-phi_bh(2));
     
   %standard deviation credit
   m_sd_b=var(m(1:6,:)).^(1/2);
   
   %standard deviation house prices
   hp_sd_b = var(p(1:6,:)).^(1/2);
   
   %additional model-implied parameters (benchmark calibration)
   pl_share(2) = pl_benchmark;
   
   %Bust/Boom household income
   re_inc_06 = alpha_h(1)*phi_bh(1)+alpha(1)+(1-alpha_h(1)-alpha(1))*phi_bl(1);
   re_inc_08 = alpha_h(2)*phi_bh(2)+alpha(2)+(1-alpha_h(2)-alpha(2))*phi_bl(2);
   income_peak_bust = re_inc_08/re_inc_06;
   
   %elasticity 
   E_06 = c*m(2,1)/(p(2,1)*N);
   
   %private-label market share
   pl_06 = 2*pl_share(1)/(m(1,1)+m(2,1)+2*pl_share(1));
   pl_07 = 2*pl_share(2)/(m(3,1)+m(4,1)+2*pl_share(2));

   %gse market share
   gse_06 = (m(1,1)+m(2,1))/(m(1,1)+m(2,1)+2*pl_share(1));
   gse_07 = (m(3,1)+m(4,1))/(m(3,1)+m(4,1)+2*pl_share(2));
   
   %cash-only house purchases
   cash_06 = 2*alpha(1)/(2*alpha(1)+2*pl_share(1)+ m(2,1)+m(1,1));
   cash_07 = 2*alpha(2)/(2*alpha(2)+2*pl_share(2)+ m(3,1)+m(4,1));
   
   
   %% Tables 1-3
   %Table 1
   FID = fopen('table_parameters.tex', 'w');
   fprintf(FID,'\\begin{tabular}{l c c c} \n');
   fprintf(FID,'\\hline \n');
   fprintf(FID,'\\hline \n');
   fprintf(FID,' Description & Symbol   & Value & Target/Source  \\\\ \n');
   fprintf(FID,'\\hline \n');
   fprintf(FID,'\\\\ \n');
   fprintf(FID,'Rich State &    &  &  \\\\ \n');
   fprintf(FID,'\\hline \n');
   fprintf(FID,'Fraction high-quality borrowers     & $\\alpha^{bh}_R$  & %8.2f & Target moments \\\\ \n',alpha_h(1));
   fprintf(FID,'Fraction non-borrowers & $\\alpha^{nb}_{R}$  & %8.2f & Cash-only purchase fraction boom \\\\ \n',alpha(1));
   fprintf(FID,'High-quality borrower shock  & $\\phi_R^{bh}$  & %8.2f & Default rate prime loans boom \\\\ \n',phi_bh(1));
   fprintf(FID,'Low-quality borrower shock  & $\\phi_R^{bl}$  & %8.2f & Default rate sub-prime loans boom \\\\ \n',phi_bl(1));
   fprintf(FID,'Fraction private-label borrowers & $s_R$  & %8.2f & Private-label share boom \\\\ \n',pl_share(1));
   fprintf(FID,'Probability rich state transition & $P$ & %8.2f & Target moments \\\\ \n',q_s(1,1));
   fprintf(FID,'\\hline \n');
   fprintf(FID,'\\\\ \n'); 
   fprintf(FID,'Poor State &    &  &  \\\\ \n');
   fprintf(FID,'\\hline \n');
   fprintf(FID,'Fraction high-quality borrowers   & $\\alpha^{bh}_P$  & %8.2f & Target moments\\\\ \n',alpha_h(2));
   fprintf(FID,'Fraction non-borrowers & $\\alpha^{nb}_{P}$  & %8.2f & Cash-only purchase fraction bust \\\\ \n',alpha(2));
   fprintf(FID,'High-quality borrower shock   & $\\phi_P^{bh}$  & %8.2f & Default rate prime loans bust \\\\ \n',phi_bh(2));
   fprintf(FID,'Low-quality borrower shock    & $\\phi_P^{bl}$  & %8.2f & Default rate sub-prime loans bust \\\\ \n',phi_bl(2));
   fprintf(FID,'Fraction private-label borrowers & $s_P$  & %8.2f & Private-label share bust \\\\ \n',pl_share(2));
   fprintf(FID,'Probability poor state transition & $P$ & %8.2f & Target moments \\\\ \n',q_s(2,2));
   fprintf(FID,'\\hline \n');
   fprintf(FID,'\\\\ \n'); 
   fprintf(FID,'Common Parameters &    &  &  \\\\ \n');
   fprintf(FID,'\\hline \n');
   fprintf(FID,'Discount factor   & $\\hat{\\beta}$   & %8.2f & Standard \\\\ \n',beta);
   fprintf(FID,'Depreciation & $\\delta$  & %8.2f & Standard \\\\ \n',delta);
   fprintf(FID,'Borrower endowment & $e_b$ & %8.2f & Target moments\\\\ \n',e);
   fprintf(FID,'Construction cost & $c$ & %8.2f & Target moments\\\\ \n',c);
   fprintf(FID,'Number of large lenders & $N$ & %8.2f & Number of GSEs\\\\ \n',N);
   fprintf(FID, '\\hline \n');
   fprintf(FID, '\\end{tabular} \n');
   fclose(FID);  

   %Table 2
   FID = fopen('table_cal.tex', 'w');
   fprintf(FID,'\\begin{tabular}{l c c} \n');
   fprintf(FID,'\\hline \n');
   fprintf(FID,'\\hline \n');
   fprintf(FID,'  & Model   & Data  \\\\ \n');
   fprintf(FID,'\\hline \n');
   fprintf(FID,'\\\\ \n');
   fprintf(FID,'GSE fraction of high-risk acquisitions 06     & %8.2f  & .16 \\\\ \n',li_fraction_06(1,1));
   fprintf(FID,'GSE fraction of high-risk acquisitions 07   & %8.2f  & .26 \\\\ \n',li_fraction_07(1,1));
   fprintf(FID,'GSE fraction of high-risk acquisitions 08   & %8.2f  & .20 \\\\ \n',li_fraction_08(1,1));
   fprintf(FID,'GSE high-risk acquisition growth 06-07   & %8.2f  & 1.04 \\\\ \n',li_gr_06_07(1,1));
   fprintf(FID,'GSE high-risk acquisition growth 07-08   & %8.2f  & -.36 \\\\ \n',li_gr_07_08(1,1));
   fprintf(FID,'House price fall 06-08   & %8.2f  & -.16 \\\\ \n',hp_fall_06_08(1,1));
   fprintf(FID,'Bust household income/Boom household income & %8.2f & .92 \\\\ \n',income_peak_bust);
   fprintf(FID,'Elasticity 06   & %8.2f   & .20 \\\\ \n',E_06);
   fprintf(FID,'Private-label Share 06  & %8.2f  & .41 \\\\ \n',pl_06);
   fprintf(FID,'Private-label Share 07 & %8.2f  & .16 \\\\ \n',pl_07);
   fprintf(FID,'GSE share 06 & %8.2f & .59 \\\\ \n',gse_06);
   fprintf(FID,'GSE share 07 & %8.2f & .84 \\\\ \n',gse_07);
   fprintf(FID,'Cash-only purchases 2006 & %8.2f & .20 \\\\ \n',cash_06);
   fprintf(FID,'Cash-only purchases 2007 & %8.2f & .25 \\\\ \n',cash_07);
   fprintf(FID, '\\hline \n');
   fprintf(FID, '\\end{tabular} \n');
   fclose(FID);
   
   %Table 3
   FID = fopen('table_counterfactuals.tex', 'w');
   fprintf(FID,'\\begin{tabular}{l c c } \n');
   fprintf(FID,'\\hline \n');
   fprintf(FID,'\\hline \n');
   fprintf(FID,'  & Benchmark &  Counterfactual 1 \\\\ \n');
   fprintf(FID,'\\hline \n');
   fprintf(FID,'\\\\ \n');
   fprintf(FID,'GSE fraction of high-risk acquisitions 06  & %8.2f& %8.2f   \\\\ \n',li_fraction_06(1,1:2));
   fprintf(FID,'GSE fraction of high-risk acquisitions 07 & %8.2f& %8.2f  \\\\ \n',li_fraction_07(1,1:2));
   fprintf(FID,'GSE fraction of high-risk acquisitions 08 & %8.2f& %8.2f   \\\\ \n',li_fraction_08(1,1:2));
   fprintf(FID,'House price fall 06-08 & %8.2f& %8.2f     \\\\ \n',hp_fall_06_08(1,1:2));
   fprintf(FID,'GSE acquisitions 06   & %8.2f & %8.2f    \\\\ \n',acquisitions_06(1,1:2));
   fprintf(FID,'GSE acquisitions 07  & %8.2f & %8.2f \\\\ \n',acquisitions_07(1,1:2));
   fprintf(FID,'GSE mortgage defaults 06 vintage &  %8.2f  & %8.2f   \\\\ \n',def_06(1,1:2));
   fprintf(FID,'GSE mortgage defaults 07 vintage & %8.2f  & %8.2f  \\\\ \n',def_07(1,1:2));
   fprintf(FID, '\\hline \n');
   fprintf(FID,'  & Counterfactual 2 & Counterfactual 3 \\\\ \n');
   fprintf(FID, '\\hline \n');
   fprintf(FID,'\\\\ \n');
   fprintf(FID,'GSE fraction of high-risk acquisitions 06  & %8.2f& %8.2f   \\\\ \n',li_fraction_06(1,3:4));
   fprintf(FID,'GSE fraction of high-risk acquisitions 07 & %8.2f& %8.2f   \\\\ \n',li_fraction_07(1,3:4));
   fprintf(FID,'GSE fraction of high-risk acquisitions 08 & %8.2f& %8.2f    \\\\ \n',li_fraction_08(1,3:4));
   fprintf(FID,'House price fall 06-08 & %8.2f& %8.2f     \\\\ \n',hp_fall_06_08(1,3:4));
   fprintf(FID,'GSE acquisitions 06   & %8.2f & %8.2f  \\\\ \n',acquisitions_06(1,3:4));
   fprintf(FID,'GSE acquisitions 07  & %8.2f & %8.2f\\\\ \n',acquisitions_07(1,3:4));
   fprintf(FID,'GSE mortgage defaults 06 vintage &  %8.2f  & %8.2f   \\\\ \n',def_06(1,3:4));
   fprintf(FID,'GSE mortgage defaults 07 vintage & %8.2f  & %8.2f \\\\ \n',def_07(1,3:4));
   fprintf(FID, '\\hline \n');
   fprintf(FID, '\\end{tabular} \n');
   fclose(FID);  
      %% Welfare analysis

gamma = .95;
beta = beta/N; %convert beta hat back to beta

for j=1:3 %3 welfare scenarios for social planner
    if j == 1
        d = 50;  %Welfare 1 - high gamma and d
    end
    if j == 2
        d = 320;  %Welfare 2 - social planner matches high risk acquisitions in 07
    end
    if j == 3
        d = 2100;  %Welfare 3 - social planner matches high risk acquisitions in 06
    end

%SP optimal lending in high state
mh_h_sp_temp =  min(gamma/(beta*d*(q_s(1,1)*(1-phi_bh(1))+q_s(1,2)*(1-phi_bh(2)))^2),alpha_h(1));
ml_h_sp_temp = min(gamma/(beta*d*(q_s(1,1)*(1-phi_bl(1))+q_s(1,2)*(1-phi_bl(2)))^2),1-alpha(1)-pl_share(1)-alpha_h(1));

%SP optimal lending in low state
mh_l_sp_temp =    min(gamma/(beta*d*(q_s(2,1)*(1-phi_bh(1))+q_s(2,2)*(1-phi_bh(2)))^2),alpha_h(2));
ml_l_sp_temp=min(gamma/(beta*d*(q_s(2,1)*(1-phi_bl(1))+q_s(2,2)*(1-phi_bl(2)))^2),1-alpha(2)-pl_share(2)-alpha_h(2));

%SP lending given sequence of 2 rich shocks and 4 poor shocks
m_h_sp(j,:) = [mh_h_sp_temp mh_h_sp_temp mh_l_sp_temp mh_l_sp_temp mh_l_sp_temp mh_l_sp_temp];
m_l_sp(j,:) = [ml_h_sp_temp ml_h_sp_temp ml_l_sp_temp ml_l_sp_temp ml_l_sp_temp ml_l_sp_temp];
m_sp(j,:) = m_h_sp(j,:)+m_l_sp(j,:);

%SP welfare given sequence of shocks

%welfare from home ownership gain
ho_sp = gamma*(m_h_sp(j,1)+m_l_sp(j,1)+beta*(m_h_sp(j,2)+m_l_sp(j,2))+beta^2*(m_h_sp(j,3)+m_l_sp(j,3))+beta^3*(m_h_sp(j,4)+m_l_sp(j,4))+beta^4*(m_h_sp(j,5)+m_l_sp(j,5))+beta^5*(m_h_sp(j,6)+m_l_sp(j,6)));
dc_sp=0;
for i=1:2
  dc_sp = dc_sp+d/2*beta^i*(q_s(1,1)*(1-phi_bh(1))*m_h_sp(j,i)+q_s(1,2)*(1-phi_bh(2))*m_h_sp(j,i))^2+d/2*beta^i*(q_s(1,1)*(1-phi_bl(1))*m_l_sp(j,i)+q_s(1,2)*(1-phi_bl(2))*m_l_sp(j,i))^2;
end
for i=3:6
  dc_sp = dc_sp+d/2*beta^i*(q_s(2,1)*(1-phi_bh(1))*m_h_sp(j,i)+q_s(2,2)*(1-phi_bh(2))*m_h_sp(j,i))^2+d/2*beta^i*(q_s(2,1)*(1-phi_bl(1))*m_l_sp(j,i)+q_s(2,2)*(1-phi_bl(2))*m_l_sp(j,i))^2;
end
welfare_sp(j) = ho_sp-dc_sp;

%welfare in benchmark calibration and counterfactual 1
ho_b(j) = gamma*(m_h(1,1)+m_l(1,1)+beta*(m_h(2,1)+m_l(2,1))+beta^2*(m_h(3,1)+m_l(3,1))+beta^3*(m_h(4,1)+m_l(4,1))+beta^4*(m_h(5,1)+m_l(5,1))+beta^5*(m_h(6,1)+m_l(6,1)));
ho_c1(j) = gamma*(m_h(1,2)+m_l(1,2)+beta*(m_h(2,2)+m_l(2,2))+beta^2*(m_h(3,2)+m_l(3,2))+beta^3*(m_h(4,2)+m_l(4,2))+beta^4*(m_h(5,2)+m_l(5,2))+beta^5*(m_h(6,2)+m_l(6,2)));

dc_b(j)=0;
dc_c1(j)=0;

for i=1:2
  dc_b(j) = dc_b(j)+d/2*beta^i*(q_s(1,1)*(1-phi_bh(1))*m_h(i,1)+q_s(1,2)*(1-phi_bh(2))*m_h(i,1))^2+d/2*beta^i*(q_s(1,1)*(1-phi_bl(1))*m_l(i,1)+q_s(1,2)*(1-phi_bl(2))*m_l(i,1))^2;
  dc_c1(j) = dc_c1(j)+d/2*beta^i*(q_s(1,1)*(1-phi_bh(1))*m_h(i,2)+q_s(1,2)*(1-phi_bh(2))*m_h(i,2))^2+d/2*beta^i*(q_s(1,1)*(1-phi_bl(1))*m_l(i,2)+q_s(1,2)*(1-phi_bl(2))*m_l(i,2))^2;
end
for i=3:6
  dc_b(j) = dc_b(j)+d/2*beta^i*(q_s(2,1)*(1-phi_bh(1))*m_h(i,1)+q_s(2,2)*(1-phi_bh(2))*m_h(i,1))^2+d/2*beta^i*(q_s(2,1)*(1-phi_bl(1))*m_l(i,1)+q_s(2,2)*(1-phi_bl(2))*m_l(i,1))^2;
  dc_c1(j) = dc_c1(j)+d/2*beta^i*(q_s(2,1)*(1-phi_bh(1))*m_h(i,2)+q_s(2,2)*(1-phi_bh(2))*m_h(i,2))^2+d/2*beta^i*(q_s(2,1)*(1-phi_bl(1))*m_l(i,2)+q_s(2,2)*(1-phi_bl(2))*m_l(i,2))^2;
end

welfare(j,1) = ho_b(j)-dc_b(j);
welfare(j,2) = ho_c1(j)-dc_c1(j);   
end
   %SP quantities 
   %gse acquisitions 06, 07 and 08
   sp_credit_06 = m_l_sp(:,1) + m_l_sp(:,2)+m_h_sp(:,1)+m_h_sp(:,2);
   sp_credit_07 = m_l_sp(:,3) + m_l_sp(:,4)+m_h_sp(:,3)+m_h_sp(:,4);
   sp_credit_08 = m_l_sp(:,5) + m_l_sp(:,6)+m_h_sp(:,5)+m_h_sp(:,6);
   
   %defaults 06, 07 and 08
   sp_default_06 = (m_l_sp(:,1) + m_l_sp(:,2))*(1-phi_bl(2))+(m_h_sp(:,1)+m_h_sp(:,2))*(1-phi_bh(2));
   sp_default_07 = (m_l_sp(:,3) + m_l_sp(:,4))*(1-phi_bl(2))+(m_h_sp(:,3)+m_h_sp(:,4))*(1-phi_bh(2));
   sp_default_08 = (m_l_sp(:,5) + m_l_sp(:,6))*(1-phi_bl(2))+(m_h_sp(:,5)+m_h_sp(:,6))*(1-phi_bh(2));

   %house price fall 06-08
   sp_hp_high_state = c*(m_l_sp(:,1)+m_h_sp(:,1)+alpha(1)+pl_share(1));
   sp_hp_low_state = c*(m_l_sp(:,6)+m_h_sp(:,6)+alpha(2)+pl_share(2));
   sp_hp_fall06_08 = (sp_hp_low_state-sp_hp_high_state)./sp_hp_high_state;
   
 for j = 1:3  
   %house price standard deviaition
   sp_hp(j,:) = [sp_hp_high_state(j) sp_hp_high_state(j) sp_hp_low_state(j) sp_hp_low_state(j) sp_hp_low_state(j) sp_hp_low_state(j)];
   hp_sd_sp(j) = var(sp_hp(j,:))^(1/2);

   %standard deviation of credit
   m_sd_sp(j) = var(m_sp(j,1:6))^(1/2);
 end

%%
%Table 4 

FID = fopen('table_welfare.tex', 'w');
   fprintf(FID,'\\begin{tabular}{l c c c} \n');
   fprintf(FID,'\\hline \n');
   fprintf(FID,'\\hline \n');
   fprintf(FID,'  & Benchmark & Counterfactual 1 &  \\\\ \n');
   fprintf(FID,'\\hline \n');
   fprintf(FID,'\\\\ \n');
   fprintf(FID,'GSE acquisitions 06  & %8.2f& %8.2f  &  \\\\ \n',acquisitions_06(1,1:2));
   fprintf(FID,'GSE acquisitions 07 & %8.2f& %8.2f   &  \\\\ \n',acquisitions_07(1,1:2));
   fprintf(FID,'GSE acquisitions 08 & %8.2f& %8.2f   &  \\\\ \n',acquisitions_08(1,1:2));
   fprintf(FID,'GSE mortgage defaults 06 vintage  & %8.2f& %8.2f  &  \\\\ \n',def_06(1,1:2));
   fprintf(FID,'GSE mortgage defaults 07 vintage & %8.2f& %8.2f   &   \\\\ \n',def_07(1,1:2));
   fprintf(FID,'GSE mortgage defaults 08 vintage & %8.2f& %8.2f   &  \\\\ \n',def_08(1,1:2));
   fprintf(FID,'House price fall 06-08 & %8.2f& %8.2f  &   \\\\ \n',hp_fall_06_08(1,1:2));
   fprintf(FID,'Standard deviation of credit  & %8.2f & %8.2f& \\\\ \n',m_sd_b(1,1:2));
   fprintf(FID,'Standard deviation of house prices &  %8.2f  & %8.2f &  \\\\ \n',hp_sd_b(1,1:2));
   fprintf(FID,'Welfare -- high $\\gamma/d$   & %8.2f & %8.2f  &  \\\\ \n',welfare(1,1:2));
   fprintf(FID,'Welfare -- medium $\\gamma/d$   & %8.2f & %8.2f  &  \\\\ \n',welfare(2,1:2));
   fprintf(FID,'Welfare -- low $\\gamma/d$  & %8.2f & %8.2f  &  \\\\ \n',welfare(3,1:2));
   fprintf(FID, '\\hline \n');
   fprintf(FID,'&  Social Planner 1 & Social Planner 2 & Social Planner 3  \\\\ \n');
      fprintf(FID,'\\hline \n');
   fprintf(FID,'\\\\ \n');
   fprintf(FID,'GSE acquisitions 06  & %8.2f& %8.2f  & %8.2f \\\\ \n',sp_credit_06);
   fprintf(FID,'GSE acquisitions 07 & %8.2f& %8.2f   & %8.2f \\\\ \n',sp_credit_07);
   fprintf(FID,'GSE acquisitions 08 & %8.2f& %8.2f   & %8.2f \\\\ \n',sp_credit_08);
   fprintf(FID,'GSE mortgage defaults 06 vintage  & %8.2f& %8.2f  & %8.2f \\\\ \n',sp_default_06);
   fprintf(FID,'GSE mortgage defaults 07 vintage & %8.2f& %8.2f   & %8.2f  \\\\ \n',sp_default_07);
   fprintf(FID,'GSE mortgage defaults 08 vintage & %8.2f& %8.2f   & %8.2f \\\\ \n',sp_default_08);
   fprintf(FID,'House price fall 06-08 & %8.2f& %8.2f  & %8.2f  \\\\ \n',sp_hp_fall06_08);
   fprintf(FID,'Standard deviation of credit  & %8.2f & %8.2f&%8.2f \\\\ \n',m_sd_sp);
   fprintf(FID,'Standard deviation of house prices &  %8.2f  & %8.2f & %8.2f \\\\ \n',hp_sd_sp);
   fprintf(FID,'Welfare -- high $\\gamma/d$   & %8.2f & N/A  & N/A \\\\ \n',welfare_sp(1));
   fprintf(FID,'Welfare -- medium $\\gamma/d$   & N/A & %8.2f  & N/A \\\\ \n',welfare_sp(2));
   fprintf(FID,'Welfare -- low $\\gamma/d$  & N/A & N/A  & %8.2f \\\\ \n',welfare_sp(3));
   fprintf(FID,'\\hline \n'); 
   fprintf(FID, '\\end{tabular} \n');
   fclose(FID);  
  

 